namespace CISLib.EncryptClasses
{
    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;

    public class MD5Encrypt
    {
        private MD5 md5 = new MD5CryptoServiceProvider();

        public string GetMD5FromFile(string filePath)
        {
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException("指定的文件没有找到");
            }
            FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            string str = new StreamReader(stream, Encoding.Unicode).ReadToEnd();
            byte[] toHash = Encoding.Unicode.GetBytes(str);
            byte[] hashed = this.md5.ComputeHash(toHash, 0, toHash.Length);
            stream.Close();
            return Encoding.ASCII.GetString(hashed);
        }

        public string GetMD5FromString(string str)
        {
            byte[] toCompute = Encoding.Unicode.GetBytes(str);
            byte[] hashed = this.md5.ComputeHash(toCompute, 0, toCompute.Length);
            return Encoding.ASCII.GetString(hashed);
        }
    }
}

